<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml" xmlns:v-model="http://www.w3.org/1999/xhtml"
      xmlns:v-for="http://www.w3.org/1999/xhtml" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../../js/vue/vue.js"></script>
    <script src="../../js/jquery/jquery.js"></script>
</head>
<body>

<div id="app">
    <form id="formId">
        <input type="hidden" name="id" v-model:value="emps.id">
        名称：<input type="text" name="name" v-model="emps.name"><br>
        年龄：<input type="text" name="age" v-model="emps.age"><br>

        <input type="button" id="btn" @click="submitForm()" value="提交"></input>
    </form>
</div>
<script>

    //获取url上的请求参数
    function getParams() {
        //获取问号及问号后面的内容
        //id=6
        var url = window.location.search;
        //创建一个Object对象
        var params = new Object();
        //判断如果 ? 号后面有值
        if (url.indexOf("?") != -1) {
            //截取问号后面的内容,再使用&分割多个属性
            var arr = url.substr(1).split("&");
            for (var i = 0; i < arr.length; i++) {
                //使用=分割为keyvalue
                // id = 6 -- [id,4]
                // name=dafei -- [name,dafei]
                // age = 18 -- [age,18]
                var keyValue = arr[i].split("=");
                //params[id] = 6
                params[keyValue[0]] = keyValue[1];
            }
        }
        return params;
    }

    console.log(getParams());


    var vue = new Vue({
        el:'#app',
        data:{
            emps:[]
        },

        methods:{
            submitForm:function () {
                var _this = this;
               //发送异步请求，进行添加操作
                $.ajax({
                   url:"http://localhost:8080/employees/edit",
                   type:"PUT",
                   data:$('#formId').serialize(),
                    success:function (data) {
                        if (data.code == 200){
                            window.location.href="list.html";
                        }else {
                            alert(data.msg);
                        }
                    }
                });
            }
        },
        mounted:function () {
            var params = getParams();

            var _this = this;
            //发送异步请求，到后台接口进行数据的查询
            $.get("http://localhost:8080/employees/detail",{id:params.id},function (data) {
                //接口返回的数据，回显到页面
                _this.emps=data;
            })
        }
    })
</script>
</body>
</html>